﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Mvc;
using FitnessDiary.WebUI.ViewHelpers;
using FitnessDiary.WebUI.Models;
using System.Collections.Generic;
using FitnessDiary.Domain.Entities;

namespace FitnessDiary.UnitTests.ViewHelpers
{
	[TestClass]
	public class DiaryHelperTest
	{
		[TestMethod]
		public void TestCreateStatIds()
		{
			Day day = CreateDay(5);

			Dictionary<int, int> result = DiaryHelper.CreateStatIds(day);

			Assert.AreEqual(2, result[1]);
			Assert.AreEqual(1, result[2]);
		}

		[TestMethod]
		public void TestCreateLinks()
		{
			Day day = CreateDay(3);

			List<TagBuilder> result = DiaryHelper.CreateLinks(day);

			Assert.AreEqual("a", result[0].TagName);
			Assert.AreEqual("deadlift x2", result[0].InnerHtml);
			Assert.AreEqual("/Hit/StatOnDay?stat=1&date=2013-10-03", result[0].Attributes["href"]);
			Assert.AreEqual(result.Count, 2);
		}

		private Day CreateDay(int dayInDate)
		{
			Day day = new Day { Hits = new List<Hit>(), Date = new DateTime(2013, 10, dayInDate) };

			Unit unit1 = new Unit { Name = "number", Abbreviation = "" };
			Unit unit2 = new Unit { Name = "kilogram", Abbreviation = "kg" };
			Unit unit3 = new Unit { Name = "kilometer", Abbreviation = "km" };

			Quantity quantity1 = new Quantity { Name = "weight", Unit = unit2 };
			Quantity quantity2 = new Quantity { Name = "reps", Unit = unit1 };
			Quantity quantity3 = new Quantity { Name = "distance", Unit = unit3 };

			Stat stat1 = new Stat { Id = 1, Name = "deadlift", Quantities = new List<Quantity>() };
			Stat stat2 = new Stat { Id = 2, Name = "running", Quantities = new List<Quantity>() };
			stat1.Quantities.Add(quantity1);
			stat1.Quantities.Add(quantity2);
			stat2.Quantities.Add(quantity3);

			Hit hit1 = new Hit() { Date = new DateTime(2013, 10, dayInDate), Stat = stat1, HitComponents = new List<HitComponent>() };
			Hit hit2 = new Hit() { Date = new DateTime(2013, 10, dayInDate), Stat = stat1, HitComponents = new List<HitComponent>() };
			Hit hit3 = new Hit() { Date = new DateTime(2013, 10, dayInDate), Stat = stat2, HitComponents = new List<HitComponent>() };

			hit1.HitComponents.Add(new HitComponent { Amount = 50, Quantity = quantity1 });
			hit1.HitComponents.Add(new HitComponent { Amount = 10, Quantity = quantity2 });
			hit2.HitComponents.Add(new HitComponent { Amount = 66, Quantity = quantity1 });
			hit2.HitComponents.Add(new HitComponent { Amount = 8, Quantity = quantity2 });
			hit3.HitComponents.Add(new HitComponent { Amount = 2, Quantity = quantity3 });

			day.Hits.Add(hit1);
			day.Hits.Add(hit2);
			day.Hits.Add(hit3);

			return day;
		}
	}
}
